package com.mocha.nezha.nezha;

import com.mocha.nezha.dmo.VehicleBO;
import com.mocha.nezha.dmo.VehicleTrackDTO;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author songkui
 * @since 2024/3/21 9:06
 */
public class VehicleManager {

    //key: parkCode;  v：VehicleCache
    private final ConcurrentHashMap<String, VehicleCache> vehicleCache = new ConcurrentHashMap<>();

    private VehicleManager(){}

    private static final class ManagerHolder {
        static final VehicleManager manager = new VehicleManager();
    }
    public static VehicleManager getInstance(){
        return ManagerHolder.manager;
    }

    public VehicleBO getVehicleByDTO(String parkCode, VehicleTrackDTO trackDTO){
        VehicleCache cache = vehicleCache.computeIfAbsent(parkCode, v -> new VehicleCache(parkCode));
        return cache.getVehicle(parkCode, trackDTO);
    }

    public VehicleBO getVehicle(String parkCode, String objectId){
        VehicleCache cache = vehicleCache.get(parkCode);
        return null == cache ? null : cache.getCache().getIfPresent(objectId);
    }


}
